/*********************************************************************
 * PoolWorker.java created on Feb 3, 2010
 *
 * This file is part of Sure Tech Support Portal System. 
 * Copyright @ 2010 Sure Tech (HK) Limited. All rights reserved.
 * Sure Tech PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *********************************************************************/
package test.com.bizdigger.thread.pool;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bizdigger.thread.pool.ThreadPool;
import com.bizdigger.thread.pool.WorkerThread;

/**
 * @Author: Mac Wang
 * @Date: Feb 3, 2010 5:03:58 PM 
 * @Email: mac@suretech.com.hk
 */

public class PoolWorker implements Runnable {
	private Log log = LogFactory.getLog(PoolWorker.class);
	
	private ThreadPool pool = null;
	private int id = 0;
	
	public PoolWorker(ThreadPool pool, int id)
	{
		this.pool = pool;
		this.id = id;
	}
	
	public void run()
	{
		try
		{
			log.debug("**** Running Tester Thread = " + id);
			
			WorkerThread thread = (WorkerThread)pool.borrowObject();
			
			Object synObj = new Object();
			Object[] params = new Object[] { "Hello", Integer.valueOf(id) };
			Class[] parmTypes = new Class[] { String.class, int.class };

			thread.execute("com.findonnet.services.pooling.test.SampleWork",
	               "executeTask", params, parmTypes, synObj);
			thread.execute("com.findonnet.services.pooling.test.SampleWork",
	          "executeTask", null, null, synObj);
			
			synchronized (synObj) {
				synObj.wait();
			}

			pool.returnObject(thread);
			log.debug("*** Finished Thread " + id);
		}
		catch(Exception e)
		{
			log.error("", e);
		}
	}
}
